﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
using RuoVea.OmiUI;
using RuoVea.OmiApp.Template.Attributes;
using RuoVea.OmiApp.Template.Language;
using RuoVea.OmiApp.Template.Options;
using RuoVea.OmiApp.Template.Service;
using SqlSugar;

namespace RuoVea.OmiApp.Template
{
    /// <summary>
    /// 注入扩展
    /// </summary>
    public static class TasksServiceCollectionExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="services"></param>
        /// <param name="serviceLifetime"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IServiceCollection AddDictSetup(this IServiceCollection services, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            return DI(services, null, serviceLifetime);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="services"></param>
        /// <param name="config"></param>
        /// <param name="serviceLifetime"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IServiceCollection AddDictSetup(this IServiceCollection services, IConfiguration config, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            if (config == null) throw new ArgumentNullException(nameof(config));
            services.Configure<AppConfig>(config);
            AppConfig appConfig = services.BuildServiceProvider().GetRequiredService<IOptions<AppConfig>>()?.Value;
            return DI(services, appConfig, serviceLifetime);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="services"></param>
        /// <param name="config"></param>
        /// <param name="serviceLifetime"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IServiceCollection AddDictSetup(this IServiceCollection services, Action<AppConfig> config, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            if (config == null) throw new ArgumentNullException(nameof(config));
            services.Configure(config);
            AppConfig appConfig = services.BuildServiceProvider().GetRequiredService<IOptions<AppConfig>>()?.Value;
            return DI(services, appConfig, serviceLifetime);
        }

        #region DI
        private static IServiceCollection DI(IServiceCollection services, AppConfig appConfig, ServiceLifetime serviceLifetime)
        {
            if (appConfig != null)
            {
                if (!string.IsNullOrWhiteSpace(appConfig.PrefixRoute))
                {
                    if (appConfig.PrefixRoute.IndexOf('/') != 0)
                    {
                        throw new ArgumentNullException(i18n.prefixroute);
                    }
                }
            }
            ConfigManages.Config = appConfig;
            //关闭参数自动校验,我们需要返回自定义的格式
            services.Configure<ApiBehaviorOptions>((o) => o.SuppressModelStateInvalidFilter = true);


            switch (serviceLifetime)
            {
                case ServiceLifetime.Scoped:
                    services.AddScoped<ISysDictTypService, SysDictTypService>();
                    services.AddScoped<ISysDictValueService, SysDictValueService>();
                    break;
                case ServiceLifetime.Singleton:
                    services.AddSingleton<ISysDictTypService, SysDictTypService>();
                    services.AddSingleton<ISysDictValueService, SysDictValueService>();
                    break;
                case ServiceLifetime.Transient:
                    services.AddTransient<ISysDictTypService, SysDictTypService>();
                    services.AddTransient<ISysDictValueService, SysDictValueService>();
                    break;
            }

            services.AddControllersWithViews().AddRazorRuntimeCompilation(option =>
            {
                option.FileProviders.Add(new EmbeddedFileProvider(typeof(Controllers.DictTypController).GetTypeInfo().Assembly));
                option.FileProviders.Add(new EmbeddedFileProvider(typeof(Controllers.DictValueController).GetTypeInfo().Assembly));
            });
            return services;
        }
        #endregion

        /// <summary>
        /// 页面UI
        /// </summary>
        /// <param name="app"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseDictUI(this IApplicationBuilder app)
        {
            app.UseOmiUIStaticAssets();
            app.UseStaticFiles(new StaticFileOptions { FileProvider = new EmbeddedFileProvider(typeof(Controllers.DictTypController).GetTypeInfo().Assembly) });
            app.UseStaticFiles(new StaticFileOptions { FileProvider = new EmbeddedFileProvider(typeof(Controllers.DictValueController).GetTypeInfo().Assembly) });
            return app;
        }

    }
}
